"""
Case Type   : rowcount_big()函数
Case Name   : 多会话交叉测试rowcount_big()函数
Create At   : 2025/07/30
Owner       : zym_2025
Description :
    1、session1建表并插入数据;
    2、session2执行rowcount_big();
    3、session2查表后执行rowcount_big();
    4、session1插入数据执行rowcount_big();
    5、session2删表执行rowcount_big();
    6、清理环境;
Expect      :
    1、建表成功,插入数据成功;
    2、返回0;
    3、返回1;
    4、返回9;
    5、返回5;
    6、清理环境成功;
History     :
"""

import unittest
import time
from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

class RowcountBig(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info('--OpenGauss_Compatibility_Function_Rowcount_Big_Case0019 start--')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.db_name = 'db_d'
        self.table_name = 't_rowcount_0019'

    def testRowcountBig(self):
        self.log.info('------创建D库并创建shark插件------')
        create_db = f"drop database if exists {self.db_name};create database {self.db_name} dbcompatibility 'D';"
        res = self.comsh.execut_db_sql(create_db)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res, '创建D库失败')
        create_ext = f"create extension shark;"
        res = self.comsh.execut_db_sql(create_ext, dbname=self.db_name)
        self.assertIn(self.constant.create_extension_success, res, '创建shark插件失败')

        text = '------step1:session1建表并插入数据,插入数据并执行rowcount_big(); expect:建表且插入数据成功,函数返回9------'
        self.log.info(text)
        sql1 = f'''drop table if exists {self.table_name};
                   create table {self.table_name} (id int, name varchar(20));
                   insert into {self.table_name} values(1, 'zhangsan');
                   select pg_sleep(10);
                   insert into t_rowcount_0019 values(generate_series (2,10), 'abc'||generate_series (2,10));
                   select rowcount_big();'''
        session1 = ComThread(self.comsh.execut_db_sql, args=(sql1, '', self.db_name))
        session1.setDaemon(True)

        text = '------step2:session2执行rowcount_big(); expect:返回0------'
        self.log.info(text)
        sql2 = f'''select rowcount_big();
                   select * from {self.table_name};
                   select rowcount_big();
                   select pg_sleep(20);
                   delete from {self.table_name} where id > 5;
                   select rowcount_big();'''
        session2 = ComThread(self.comsh.execut_db_sql, args=(sql2, '', self.db_name))
        session2.setDaemon(True)
        session1.start()
        time.sleep(2)
        session2.start()
        session1.join(18)
        session2.join(30)
        res1 = session1.get_result()
        res2 = session2.get_result()
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, res1, '建表失败')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res1, '插入数据失败')
        self.assertTrue('9' in res1.splitlines()[-2], 'session1 rowcount_big()查询异常')
        self.assertTrue('0' in res2.splitlines()[2], 'session2 rowcount_big()初次查询返回异常')
        self.assertTrue('1' in res2.splitlines()[12], 'session2 rowcount_big()查表后查询异常')
        self.assertTrue('5' in res2.splitlines()[-2], 'session2 rowcount_big()删表后查询异常')


    def tearDown(self):
        text = '------step3:清理环境;expect:成功------'
        self.log.info(text)
        del_sql = f"drop table if exists {self.table_name};"
        res = self.comsh.execut_db_sql(del_sql, dbname=self.db_name)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, res, '删表失败')

        drop_sql = f"drop database {self.db_name};"
        res = self.comsh.execut_db_sql(drop_sql)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res, '删除D库失败')
        self.log.info(f'------OpenGauss_Compatibility_Function_Rowcount_Big_Case0019 end------')